package com.qinggeng.project.system.service.impl;

import java.util.List;

import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.project.api.dto.GoodsEvaluateDto;
import com.qinggeng.project.api.dto.UserEvaluateDto;
import com.qinggeng.project.api.dto.UserEvaluateGoodsDto;
import com.qinggeng.project.api.utils.SensitiveWordFilterUtils;
import com.qinggeng.project.system.domain.GoodsDefaultEvaluate;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.domain.OrdersGoods;
import com.qinggeng.project.system.mapper.GoodsDefaultEvaluateMapper;
import com.qinggeng.project.system.mapper.OrdersGoodsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qinggeng.project.system.mapper.OrdersEvaluateMapper;
import com.qinggeng.project.system.domain.OrdersEvaluate;
import com.qinggeng.project.system.service.IOrdersEvaluateService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 订单评价Service业务层处理
 *
 * @author qinggeng
 * @date 2020-06-19
 */
@Service
@Slf4j
public class OrdersEvaluateServiceImpl implements IOrdersEvaluateService
{
    @Resource
    private OrdersEvaluateMapper ordersEvaluateMapper;

    @Resource
    private OrdersGoodsMapper ordersGoodsMapper;

    @Resource
    private GoodsDefaultEvaluateMapper defaultEvaluateMapper;

    /**
     * 查询订单评价
     *
     * @param ordersEvaluateId 订单评价ID
     * @return 订单评价
     */
    @Override
    public OrdersEvaluate selectOrdersEvaluateById(Long ordersEvaluateId)
    {
        return ordersEvaluateMapper.selectOrdersEvaluateById(ordersEvaluateId);
    }

    /**
     * 查询订单评价列表
     *
     * @param ordersEvaluate 订单评价
     * @return 订单评价
     */
    @Override
    public List<OrdersEvaluate> selectOrdersEvaluateList(OrdersEvaluate ordersEvaluate)
    {
        return ordersEvaluateMapper.selectOrdersEvaluateList(ordersEvaluate);
    }

    /**
     * 新增订单评价
     *
     * @param ordersEvaluate 订单评价
     * @return 结果
     */
    @Override
    public int insertOrdersEvaluate(OrdersEvaluate ordersEvaluate)
    {
        ordersEvaluate.setCreateTime(DateUtils.getNowDate());
        return ordersEvaluateMapper.insertOrdersEvaluate(ordersEvaluate);
    }

    /**
     * 修改订单评价
     *
     * @param ordersEvaluate 订单评价
     * @return 结果
     */
    @Override
    public int updateOrdersEvaluate(OrdersEvaluate ordersEvaluate)
    {
        return ordersEvaluateMapper.updateOrdersEvaluate(ordersEvaluate);
    }

    /**
     * 批量删除订单评价
     *
     * @param ordersEvaluateIds 需要删除的订单评价ID
     * @return 结果
     */
    @Override
    public int deleteOrdersEvaluateByIds(Long[] ordersEvaluateIds)
    {
        return ordersEvaluateMapper.deleteOrdersEvaluateByIds(ordersEvaluateIds);
    }

    /**
     * 删除订单评价信息
     *
     * @param ordersEvaluateId 订单评价ID
     * @return 结果
     */
    @Override
    public int deleteOrdersEvaluateById(Long ordersEvaluateId)
    {
        return ordersEvaluateMapper.deleteOrdersEvaluateById(ordersEvaluateId);
    }

    @Override
    public List<GoodsEvaluateDto> selectOrdersEvaluateGoodsDtoList(OrdersEvaluate where) {
        return ordersEvaluateMapper.selectOrdersEvaluateGoodsDtoList(where);
    }

    @Override
    public List<UserEvaluateDto> selectOrdersEvaluateUserDtoList(OrdersEvaluate where) {
        return ordersEvaluateMapper.selectOrdersEvaluateUserDtoList(where);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addEvaluate(Long ordersGoodsId, Long shopId,Long userId, String evaluateDesc, String evaluateImgs, Integer goodsQuality,String delFlag) {

        OrdersGoods ordersGoods = ordersGoodsMapper.selectOrdersGoodsById(ordersGoodsId);
        if (ordersGoods == null) {
            throw new CustomException("商品不存在");
        }
        if (CommonEnum.STATUS_20.getValue().equals(ordersGoods.getEvalStatus())){
            throw new CustomException("商品已评价");
        }

        /**
         * 将评价内容保存到数据库中
         */
        try{
            OrdersEvaluate ordersEvaluate = new OrdersEvaluate();
            ordersEvaluate.setUserId(userId);
            ordersEvaluate.setShopId(shopId);
            ordersEvaluate.setOrdersId(ordersGoods.getOrdersId());
            ordersEvaluate.setGoodsId(ordersGoods.getGoodsId());
            ordersEvaluate.setGoodsQuality(goodsQuality);
            ordersEvaluate.setEvaluateImgs(evaluateImgs);
            //评价内容中敏感词过滤
            evaluateDesc=SensitiveWordFilterUtils.getWordAfterFilter(evaluateDesc);
            ordersEvaluate.setEvaluateDesc(evaluateDesc);
            ordersEvaluate.setDelFlag(delFlag);
            int rows=this.insertOrdersEvaluate(ordersEvaluate);
            if(rows<1){
                log.error("添加评价内容失败");
                throw new CustomException("添加评价内容失败", ShowTypeEnum.用户.getValue());
            }
            //订单商品评价状态更改为20=已评价
            ordersGoods.setEvalStatus(CommonEnum.STATUS_20.getValue());
            rows=ordersGoodsMapper.updateOrdersGoods(ordersGoods);
            if(rows<1){
                log.error("更改商品评价状态失败");
                throw new CustomException("更改商品评价状态失败",ShowTypeEnum.用户.getValue());
            }

        }catch (Exception e){
            log.error(e.getMessage());
            throw new CustomException("添加评价内容失败",ShowTypeEnum.用户.getValue());
        }

    }


    @Override
    public void addEvaluate(Orders orders){

        List<OrdersGoods> ordersGoodsList=ordersGoodsMapper.selectOrderGoodListByOrdersId(orders.getOrdersId());
        if(CollectionUtils.isEmpty(ordersGoodsList)){
            log.error("订单商品为空");
            throw new CustomException("订单商品为空");
        }
        String evaluateDesc="系统默认好评！";
        try{
            for (OrdersGoods ordersGoods:ordersGoodsList
            ) {
                if(ordersGoods.getEvalStatus().equals(CommonEnum.STATUS_20.getValue())){
                    continue;
                }
                String evaluate=defaultEvaluateMapper.getGoodsDefaultEvaluateByRandom();
                if(StringUtils.isNotEmpty(evaluate)){
                    evaluateDesc=evaluate;
                }
                this.addEvaluate(ordersGoods.getOrdersGoodsId(),orders.getShopId(),orders.getUserId(),evaluateDesc,null,5,"2");
            }
        }catch (Exception e){
            log.info(e.getMessage());
            throw new CustomException("批量评价失败！");
        }
    }
}
